package com.xiangyueEducation.uploaderCloud.mq.batchImport;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiangyueEducation.uploaderCloud.pojo.entity.User;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@Slf4j
public class BatchImportProducer {

    @Resource(name = "batchImportExchange")
    DirectExchange directExchange;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    ObjectMapper objectMapper;
    public void send(User user){
        try {
            String s = objectMapper.writeValueAsString(user);
            rabbitTemplate.convertAndSend(directExchange.getName(),"batchImport-queue",s);
        } catch (JsonProcessingException e) {
            log.error("对象序列化失败{}",user,e);
            throw new RuntimeException(e);
        }
    }

}
